﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATW.MT.BUS;
using System.Data;
using System.Text;
using Newtonsoft.Json;
using System.Collections;


using ATW.MT.CommonClass;
namespace ATW.MT.SellManage
{
    public partial class ProductionArgs : System.Web.UI.Page
    {
        bus_C_Material dm = new bus_C_Material();
        bus_C_Size cs = new bus_C_Size();
        bus_C_Pressure cp = new bus_C_Pressure();
        bus_C_Conn cc = new bus_C_Conn();
        bus_C_Design cd = new bus_C_Design();
        bus_C_FranLevel cf = new bus_C_FranLevel();
        bus_C_StructLevel SL = new bus_C_StructLevel();
        bus_C_SealLevel Se = new bus_C_SealLevel();
        bus_T_ProductionArgs TP = new bus_T_ProductionArgs();
        bus_T_production pro = new bus_T_production();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["handler"] != null || Request.Form["handler"] != null)
                {
                    string handler = Request.Form["handler"] == null ? Request.QueryString["handler"] : Request.Form["handler"];
                    if (handler == "getMaterialList")
                    {
                        WriteRes(getMaterialList());
                    }
                    if (handler == "getMaterialUsed")
                    {
                        WriteRes(getMaterialUsed());
                    }
                    if (handler == "setMaterial")
                    {
                        WriteRes(setMaterial());
                    }
                    if (handler == "getSizeList")
                    {
                        WriteRes(getSizeList());
                    }
                    if (handler == "getSizeUsed")
                    {
                        WriteRes(getSizeUsed());
                    }
                    if (handler == "setSize")
                    {
                        WriteRes(setSize());
                    }
                    if (handler == "getPressureList")
                    {
                        WriteRes(getPressureList());
                    }
                    if (handler == "getPressureUsed")
                    {
                        WriteRes(setPressure());
                    }
                    if (handler == "setPressure")
                    {
                        WriteRes(savePressure());
                    }
                    if (handler == "getDesignList")
                    {
                        WriteRes(getDesignList());
                    }
                    if (handler == "getDesignUsed")
                    {
                        WriteRes(getDesignUsed());
                    }
                    if (handler == "setDesign")
                    {
                        WriteRes(saveDesign());
                    }
                    if (handler == "getConnList")
                    {
                        WriteRes(getConnList());
                    }
                    if (handler == "getConnUsed")
                    {
                        WriteRes(getConnUsed());
                    }
                    if (handler == "setConn")
                    {
                        WriteRes(saveConn());
                    }
                    if (handler == "getFranList")
                    {
                        WriteRes(getFranList());
                    }
                    if (handler == "getFranUsed")
                    {
                        WriteRes(getFranUsed());
                    }
                    if (handler == "setFran")
                    {
                        WriteRes(setFran());
                    }
                    if (handler == "getStructList")
                    {
                        WriteRes(getStructList());
                    }
                    if (handler == "getStructUsed")
                    {
                        WriteRes(getStructUsed());
                    }
                    if (handler == "setStruct")
                    {
                        WriteRes(setStruct());
                    }
                    if (handler == "getSealList")
                    {
                        WriteRes(getSealList());
                    }
                    if (handler == "getSealUsed")
                    {
                        WriteRes(getSealUsed());
                    }
                    if (handler == "setSeal")
                    {
                        WriteRes(setSeal());
                    }
                    return;
                }
                BindData();
            }
        }
        private void BindData()
        {
            try
            {
                string pid = Request.QueryString["pid"];
                if (!CommonClass.common.filterChar(pid))
                {
                    return;
                }
                DataTable dt = pro.ProductionSingleQuery(pid);
                if (dt != null && dt.Rows.Count > 0)
                {
                    lblProductionName.Text = dt.Rows[0]["Desc"].ToString();
                    hdfProid.Value = dt.Rows[0]["ProductionID"].ToString();
                }
                else
                {
                    lblProductionName.Text = "未知产品";
                }
            }
            catch (Exception ex)
            {

            }
        }
        #region 材质
        private string getMaterialList()
        {//获取材质列表
            string res = "";
            try
            {
                DataTable dt = dm.MaterialPagerQuery("", 1, int.MaxValue);
                DataView dv = dt.DefaultView;
                dv.Sort = "code_d desc";
                dt = dv.ToTable();
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return res;
        }
        private string getMaterialUsed()
        {
            string res = "";
            try
            {
                string pid = Request.QueryString["pid"];
                string type = Request.QueryString["type"];
                if (!common.filterChar(pid) || !common.filterChar(type))
                {
                    return "";
                }
                DataTable dt = TP.getProductionMaterial(pid, type);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return res;
        }
        private string setMaterial()
        {//设置
            string res = "";
            try
            {
                string pid = Request.Form["pid"];
                string type = Request.Form["type"];
                string list = Request.Form["list"];
                if (!CommonClass.common.filterChar(pid) ||
                    !CommonClass.common.filterChar(type) ||
                    !CommonClass.common.filterChar(list))
                {
                    return "illigal args";
                }

                string[] _list = list.Split(new char[] { '|' });

                #region 比对数据增加排序字段
                DataTable dt = TP.getProductionMaterialNoLan(pid, type);
                Dictionary<string, string> dic = new Dictionary<string, string>();


                foreach (DataRow dr in dt.Rows)
                {
                    foreach (string strKey in _list)
                    {
                        if (dr["MID"].ToString() == strKey.ToString() && dr["type"].ToString() == type.ToString())
                        {
                            dic.Add(strKey.ToString(), dr["RowId"].ToString());
                        }
                    }
                }



                foreach (string strKey in _list)
                {
                    if (!dic.ContainsKey(strKey))
                    {
                        dic.Add(strKey.ToString(), "9999");
                    }
                }


                #endregion

                ArrayList al = new ArrayList();
                al.Add(TP.removeProductionMaterial(pid, type));//删除

                foreach (var i in dic)
                {
                    al.Add(TP.addProductionMaterial(pid, i.Key, i.Value, type));
                }

                //string[] _list = list.Split(new char[] { '|' });
                //for (int i = 0; i < _list.Length; i++)
                //{
                //    al.Add(TP.addProductionMaterial(pid, _list[i], type));
                //}
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";
            }
            catch (Exception ex)
            {

            }
            return res;
        }
        #endregion
        #region 口径
        private string getSizeList()//获取全部口径
        {
            string res = "";
            try
            {
                DataTable dt = cs.SizePagerQuery("", 1, int.MaxValue);
                DataView dv = dt.DefaultView;
                dv.Sort = "[Size_Code_C] desc";
                dt = dv.ToTable();
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {
            }
            return res;
        }
        private string getSizeUsed()//获取已使用的口径
        {
            string res = "";
            try
            {
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(pid))
                {
                    return "";
                }
                DataTable dt = TP.getProductionSize(pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return res;
        }
        private string setSize()//设置口径
        {
            string res = "";
            try
            {
                string pid = Request.Form["pid"];
                string list = Request.Form["list"];
                if (!common.filterChar(pid) || !common.filterChar(list))
                {
                    return "illigal args";
                }
                ArrayList al = new ArrayList();
                al.Add(TP.removeProductionSize(pid));
                string[] _list = list.Split(new char[] { '|' });
                for (int i = 0; i < _list.Length; i++)
                {
                    al.Add(TP.addProductionSize(pid, _list[i]));
                }
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";

            }
            catch (Exception ex)
            {

            }
            return res;
        }
        #endregion
        #region 压力等级
        private string getPressureList()//获取全部压力等级
        {
            string res = "";
            try
            {
                DataTable dt = cp.PressPagerQuery("", 1, int.MaxValue);
                DataView dv = dt.DefaultView;
                dv.Sort = "Pressure_Code_C desc";
                dt = dv.ToTable();
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return res;
        }
        private string setPressure()//获取已使用的压力等级
        {
            string res = "";
            try
            {
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(pid))
                {
                    return "illigal args";
                }
                DataTable dt = TP.getProductionPressure(pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return res;
        }
        private string savePressure()//保存压力等级
        {
            string res = "";
            try
            {
                string pid = Request.Form["pid"];
                string list = Request.Form["list"];
                if (!common.filterChar(pid) || !common.filterChar(list))
                {
                    return "illigal args";
                }
                ArrayList al = new ArrayList();
                al.Add(TP.removeProductionPressure(pid));
                string[] _list = list.Split(new char[] { '|' });
                for (int i = 0; i < _list.Length; i++)
                {
                    al.Add(TP.addProductionPressure(pid, _list[i]));
                }
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";
            }
            catch (Exception ex)
            {

            }
            return res;
        }

        #endregion
        #region 设计标准
        public string getDesignList()
        {
            string res = "";
            try
            {
                DataTable dt = cd.DesignPagerQuery("", 1, int.MaxValue);
                DataView dv = dt.DefaultView;
                dv.Sort = "[Desc] desc";
                dt = dv.ToTable();
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return res;
        }
        public string getDesignUsed()
        {
            string res = "";
            try
            {
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(pid))
                {
                    return "illigal args";
                }
                DataTable dt = TP.getProductionDesign(pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return res;
        }
        public string saveDesign()
        {
            string res = "";
            try
            {
                string pid = Request.Form["pid"];
                string list = Request.Form["list"];
                if (!common.filterChar(pid) || !common.filterChar(list))
                {
                    return "illigal args";
                }
                ArrayList al = new ArrayList();
                al.Add(TP.removeProductionDesign(pid));
                string[] _list = list.Split(new char[] { '|' });
                for (int i = 0; i < _list.Length; i++)
                {
                    al.Add(TP.addProductionDesign(pid, _list[i]));
                }
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";
            }
            catch (Exception ex)
            {

            }
            return res;
        }
        #endregion
        #region 连接方式
        public string getConnList()
        {
            string res = "";
            try
            {
                DataTable dt = cc.ConnPagerQuery("", 1, int.MaxValue);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return res;
        }
        public string getConnUsed()
        {
            string res = "";
            try
            {
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(pid))
                {
                    return "illigal args";
                }
                DataTable dt = TP.getProductionConn(pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return res;
        }
        public string saveConn()
        {
            string res = "";
            try
            {
                string pid = Request.Form["pid"];
                string list = Request.Form["list"];
                if (!common.filterChar(pid) || !common.filterChar(list))
                {
                    return "illigal args";
                }
                ArrayList al = new ArrayList();
                al.Add(TP.removeProductionConn(pid));
                string[] _list = list.Split(new char[] { '|' });
                for (int i = 0; i < _list.Length; i++)
                {
                    al.Add(TP.addProductionConn(pid, _list[i]));
                }
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";
            }
            catch (Exception ex)
            {

            }
            return res;
        }
        #endregion
        #region 法兰标准
        public string getFranList()
        {
            string res = "";
            try
            {
                DataTable dt = cf.FranPagerQuery("", 1, int.MaxValue);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {
            }
            return res;
        }
        public string getFranUsed()
        {
            string res = "";
            try
            {
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(pid))
                {
                    return "illigal args";
                }
                DataTable dt = TP.getProductionFran(pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {
            }
            return res;
        }
        public string setFran()
        {
            string res = "";
            try
            {
                string pid = Request.Form["pid"];
                string list = Request.Form["list"];
                if (!common.filterChar(pid) || !common.filterChar(list))
                {
                    return "illigal args";
                }
                ArrayList al = new ArrayList();
                al.Add(TP.removeProductionFran(pid));
                string[] _list = list.Split(new char[] { '|' });
                for (int i = 0; i < _list.Length; i++)
                {
                    al.Add(TP.addProductionFran(pid, _list[i]));
                }
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";
            }
            catch (Exception ex)
            {
            }
            return res;
        }
        #endregion
        #region 结构标准
        public string getStructList()
        {
            string res = "";
            try
            {
                DataTable dt = SL.StructPagerQuery("", 1, int.MaxValue);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {
            }
            return res;
        }
        public string getStructUsed()
        {
            string res = "";
            try
            {
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(pid))
                {
                    return "illigal args";
                }
                DataTable dt = TP.getProductionStruct(pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {
            }
            return res;
        }
        public string setStruct()
        {
            string res = "";
            try
            {
                string pid = Request.Form["pid"];
                string list = Request.Form["list"];
                if (!common.filterChar(pid) || !common.filterChar(list))
                {
                    return "illigal args";
                }
                ArrayList al = new ArrayList();
                al.Add(TP.removeProductionStruct(pid));
                string[] _list = list.Split(new char[] { '|' });
                for (int i = 0; i < _list.Length; i++)
                {
                    al.Add(TP.addProductionStruct(pid, _list[i]));
                }
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";
            }
            catch (Exception ex)
            {
            }
            return res;
        }
        #endregion
        #region 密封等级
        public string getSealList()
        {
            string res = "";
            try
            {
                DataTable dt = Se.SealPagerQuery("", 1, int.MaxValue);
                DataView dv = dt.DefaultView;
                dv.Sort = "LevelType desc,Level desc";
                dt = dv.ToTable();
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {
            }
            return res;
        }
        public string getSealUsed()
        {
            string res = "";
            try
            {
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(pid))
                {
                    return "illigal args";
                }
                DataTable dt = TP.getProductionSeal(pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {
            }
            return res;
        }
        public string setSeal()
        {
            string res = "";
            try
            {
                string pid = Request.Form["pid"];
                string list = Request.Form["list"];
                if (!common.filterChar(pid) || !common.filterChar(list))
                {
                    return "illigal args";
                }
                ArrayList al = new ArrayList();
                al.Add(TP.removeProductionSeal(pid));
                string[] _list = list.Split(new char[] { '|' });
                for (int i = 0; i < _list.Length; i++)
                {
                    al.Add(TP.addProductionSeal(pid, _list[i]));
                }
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";
            }
            catch (Exception ex)
            {
            }
            return res;
        }
        #endregion


        private void WriteRes(string s)
        {
            Response.Clear();
            Response.Write(s);
            Response.End();
        }
        private void Tips(string s)
        {//提示
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
    }
}